<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Model\Channel;
use App\Model\LinkChannel;
use App\Model\LinkFile;
use App\Model\LinkIndustry;
use App\Model\Landmark;
use App\Model\Product;
use App\Model\ProductSpecKey;
use App\Model\ProductSpecValue;
use App\Model\Sku;
use App\Service\SkuService;
use App\Model\SkuPrice;
use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class ProductSpecValueService extends BaseService
{

    /**
     * @Inject
     * @var SkuService
     */
    private $SkuService;
    //   新增单条  包括关联关系
    /**
     * @param array $insertData
     * @return bool
     */

    public function insert(array  $insertData)
    {
        $spec_key = $sku_list = $key_arr = $detail_pic = $main_pic = $channel =  [];
        if(isset($insertData['spec_key'])){
            $spec_key = $insertData['spec_key'];unset($insertData['spec_key']);
        }
        if(isset($insertData['sku'])){
            $sku_list = $insertData['sku'];unset($insertData['sku']);
        }
        if(isset($insertData['channel'])){
            $channel = $insertData['channel'];unset($insertData['channel']);
        }

        if(isset($insertData['main_pic'])){
            $main_pic = $insertData['main_pic'];unset($insertData['main_pic']);
        }
         if(isset($insertData['detail_pic'])){
             $detail_pic = $insertData['detail_pic'];unset($insertData['detail_pic']);

         }
        $product_id = '';

        $product_id = $this->insertGetId($insertData);

        if(count($spec_key)>0){
            $key_arr = $this->spec_key_insert($spec_key,$product_id);
        }
        if(count($main_pic)>0){
            $this->link_file_insert($main_pic,$product_id,2);
        }
        if(count($detail_pic)>0){
            $this->link_file_insert($detail_pic,$product_id,3);
        }

        return $product_id;
    }

//    审核记录
    public function insert_audit(array  $insertData,$product_id,$main_pic,$detail_pic,$channel,$sku_ids,$key_arr)
    {
//        插入
//        图片
        if(count($main_pic)>0){
            $this->link_file_insert($main_pic,$product_id,4);
        }
        if(count($detail_pic)>0){
            $this->link_file_insert($detail_pic,$product_id,5);
        }
//        频道
        if(count($channel)>0){
            $this->channel_insert($channel,$product_id,2);
        }

        if(count($sku_ids)>0){
//            sku插入
            $this->SkuService->sku_audit_insert($sku_ids,$product_id,$key_arr);
        }
        return $product_id;
    }
    public function insertGetId(array  $insertData)
    {
        return Product::query()->insertGetId($insertData);
    }

    public function channel_insert(array  $channel_insert,$product_id,$type=1):bool
    {
        $arr = [];
        $LinkChannel = New LinkChannel();
        foreach ($channel_insert as $key=>$value){
            $arr[] = [
                'link_id'=>$product_id,
                'channel_id'=>$value,
                'type'=>$type,
            ];
        }
        $LinkChannel->insert($arr);
        return true;
    }

    //    关联数据插入
    public function  spec_key_insert(array  $spec_key,$product_id)
    {
        $arr = [];
        $ProductSpecKey = New ProductSpecKey();
        foreach ($spec_key as $key=>$value){

            $arr[] = $ProductSpecKey->insertGetId([
                'product_id'=>$product_id,
                'spec_name'=>$value
            ]);
        }
        return $arr;
    }
    
    //    关联文件插入
    public function  link_file_insert(array  $files,$product_id,$type=2)
    {
        //        文件
        foreach ($files as $v){
            $file_data[] = [
                'fileinfo_id'=>$v,
                'link_id'=>$product_id,
                'type'=>$type,
            ];
        }
        if(count($file_data)>0){
            LinkFile::query()->insert($file_data);
        }
    }


    //    关联数据插入
    public function  spec_value_insert(array  $skuData,$product_id)
    {
        $arr = [];
        foreach ($skuData as $key=>$value){
            $value['product_id'] = $product_id;
            $arr[] =$value;
        }
        ProductSpecValue::query()->insert($arr);
        return true;
    }

//    更新product
    public function update(array  $upData)
    {
        $spec_key = $sku_list = $key_arr = $detail_pic = $main_pic = $channel =  [];
        if(isset($upData['spec_key'])){
            $spec_key = $upData['spec_key'];unset($upData['spec_key']);

        }
        if(isset($upData['channel'])){
            $channel = $upData['channel'];unset($upData['channel']);
        }
        if(isset($upData['main_pic'])){
            $main_pic = $upData['main_pic'];unset($upData['main_pic']);
        }
        if(isset($upData['detail_pic'])){
            $detail_pic = $upData['detail_pic'];unset($upData['detail_pic']);
        }
        $product_id = '';



        if(count($spec_key)>0){
            $key_arr = $this->spec_key_insert($spec_key,$product_id);
        }
        if(count($main_pic)>0){
            $this->link_file_insert($main_pic,$product_id,2);
        }
        if(count($detail_pic)>0){
            $this->link_file_insert($detail_pic,$product_id,3);
        }
        return CostSet::query()->where('id',$upData['id'])->update($upData);
    }
}